package com.zzyl.controller;

import com.aliyun.iot20180120.models.QueryDevicePropertyStatusResponseBody;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceDataVo;
import com.zzyl.vo.ProductVo;
import com.zzyl.vo.retreat.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/iot")
@Api(tags="产品")
public class DeviceController {

    @Autowired
    DeviceService deviceService;

    @PostMapping("/syncProductList")
    @ApiOperation("查询产品并存入redis")
    public ResponseResult getProductToRedis(){
        deviceService.getProductToRedis();
        return ResponseResult.success();
    }

    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品")
    public ResponseResult<List<ProductVo>> getAllProduct(){
        List<ProductVo> list=deviceService.getAllProduct();
        return ResponseResult.success(list);
    }

    @PostMapping("/RegisterDevice")
    @ApiOperation("注册设备")
    public ResponseResult addDevice(@RequestBody DeviceDto dto){
        deviceService.addDevice(dto);
        return ResponseResult.success();
    }

    @GetMapping("/pageQueryDevice")
    @ApiOperation("查询设备")
    public ResponseResult<PageResponse<DeviceDataVo>> pageDevice(
            Integer pageNum,
            Integer pageSize,
            String deviceName,
            Integer locationType,
            String productKey
    ){
        PageResponse<DeviceDataVo> devicePage= deviceService.pageDevice(pageNum,pageSize,deviceName,locationType,productKey);
        return ResponseResult.success(devicePage);
    }

    @PostMapping("/QueryDeviceDetail")
    @ApiOperation("1.5 查询设备详细数据")
    public ResponseResult<PageResponse<DeviceVo>> getDeviceData(
          @RequestBody DeviceDto deviceDto
    ){
        DeviceVo deviceDetail= deviceService.getDeviceDetailByIotId(deviceDto);
        return ResponseResult.success(deviceDetail);
    }

    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation("查询指定设备的物模型运行状态")
    public ResponseResult<QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData>getDevice (
            @RequestBody DeviceDto deviceDto
    ){
        QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData data= deviceService.queryDevicePropertyStatus(deviceDto);
        return ResponseResult.success(data);
    }

    @PostMapping("/QueryThingModelPublished")
    @ApiOperation("1.7 查看指定产品的已发布物模型中的功能定义详情")
    public ResponseResult<Map<String,String>>getModel (
            @RequestBody DeviceDto deviceDto
    ){
        Map<String,String> map= deviceService.getModel(deviceDto);
        return ResponseResult.success(map);
    }

    @PostMapping("/UpdateDevice")
    @ApiOperation("1.7 查看指定产品的已发布物模型中的功能定义详情")
    public ResponseResult update(
            @RequestBody DeviceDto deviceDto
    ){
         deviceService.update(deviceDto);
        return ResponseResult.success();
    }
    @DeleteMapping("/DeleteDevice")
    @ApiOperation("1.9 删除设备")
    public ResponseResult delete(
            @RequestBody DeviceDto deviceDto
    ){
        deviceService.deleteDevice(deviceDto);
        return ResponseResult.success();
    }



}
